package com.chen.palmar.project.set;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.constant.AppConstant;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import io.valuesfeng.picker.ImageSelectActivity;
import io.valuesfeng.picker.Picker;
import io.valuesfeng.picker.engine.PicassoEngine;
import rx.Subscription;

/**
 * Created by chen on 2018/1/8.
 */

public class ApplyIdentityActivity extends BaseActivity {
    private static final int REQUEST_CODE_IMG = 31;
    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.et_identity_name)
    EditText etIdentityName;
    //    @Bind(R.id.et_identity_address)
    //    EditText etIdentityAddress;
    //    @Bind(R.id.et_identity_user_name)
    //    EditText etIdentityUserName;
    //    @Bind(R.id.et_identity_email)
    //    EditText etIdentityEmail;
    //    @Bind(R.id.et_identity_phone)
    //    EditText etIdentityPhone;
    @Bind(R.id.et_identity_number)
    EditText etIdentityNumber;
    @Bind(R.id.iv_identity_img)
    ImageView ivIdentityImg;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        toolBar.setNavigationOnClickListener(v -> ApplyIdentityActivity.this.finish());

        titleBar.setText("厂家认证");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_identity_apply;
    }

    @OnClick({R.id.iv_identity_img, R.id.btn_identity_info})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_identity_img:
                Picker.from(this).singleChoice().enableCamera(true).setEngine(new PicassoEngine()).forResult(REQUEST_CODE_IMG);
                break;
            case R.id.btn_identity_info:
                applyIdentityInfo();
                break;
        }
    }

    private void applyIdentityInfo() {
        String name = etIdentityName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请输入公司名称");
            return;
        }
        //        String address = etIdentityAddress.getText().toString();
        //        if (TextUtils.isEmpty(address)) {
        //            showToast("请输入公司地址");
        //            return;
        //        }
        //        String userName = etIdentityUserName.getText().toString();
        //        if (TextUtils.isEmpty(userName)) {
        //            showToast("请输入联系人姓名");
        //            return;
        //        }
        //        String email = etIdentityEmail.getText().toString();
        //        if (TextUtils.isEmpty(email)) {
        //            showToast("请输入电子邮箱");
        //            return;
        //        }
        //        String phone = etIdentityPhone.getText().toString();
        //        if (TextUtils.isEmpty(phone)) {
        //            showToast("请输入手机号码");
        //            return;
        //        }
        String number = etIdentityNumber.getText().toString();
        if (TextUtils.isEmpty(number)) {
            showToast("请输入营业执照");
            return;
        }

        if (null == uri) {
            showToast("请选择营业执照照片");
            return;
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        //        map.put("email", email);
        //        map.put("contacts", userName);
        //        map.put("mobile", phone);
        map.put("license_no", number);
        map.put("business_license", new File(FileUtils.getRealPathFromUri(this, uri)));
        Subscription subscribe = DataCenter.applyIdentityInfo(map).subscribe(new HttpSubscriber<HttpResultEntity>(this, showLoading()) {
            @Override
            public void onNext(HttpResultEntity httpResultEntity) {
                if (!TextUtils.isEmpty(httpResultEntity.getMessage())) {
                    showToast(httpResultEntity.getMessage());
                    setResult(Activity.RESULT_OK);
                    ApplyIdentityActivity.this.finish();
                }
            }
        });
        subscription.add(subscribe);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_IMG && resultCode == Activity.RESULT_OK) {
            List<Uri> imgList = data.getParcelableArrayListExtra(ImageSelectActivity.EXTRA_RESULT_SELECTION);
            uri = imgList.get(0);
            Picasso.with(this).load(uri).resize(AppConstant.SCREEN_WIDTH - 80, (int) ((AppConstant.SCREEN_WIDTH - 80) / 2)).placeholder(R.mipmap.set_head_icon).error(R.mipmap.set_head_icon).into(ivIdentityImg);
        }
    }
}
